﻿using Cxuu.Common;
using Cxuu.Models;
using PetaPoco;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Cxuu.Repository;

[AppService]
public class ContentRepository : BaseRepository<Content, PetaPocoContext>
{

    public ContentRepository(PetaPocoContext petaPocoContext) : base(petaPocoContext) { }


    /// <summary>
    /// 前台访问文章时 浏览量+1
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public async Task IncHits(int? id)
    {
        Sql sql = Sql.Builder;
        sql.Append("SET Hits = Hits + 1")
            .Where("Id =@0", id);
        await CreateDAO().UpdateAsync<Content>(sql);
    }


    /// <summary>
    /// 前台查询一条
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public async Task<ViewContentAndContent> GetAsync(int? id)
    {
        Sql sql = Sql.Builder;
        sql.Select("*")
        .From("cxuu_content AS core")
        .LeftJoin("cxuu_content_content AS content")
        .On("core.Id = content.Aid")
        .Where("core.Id =@0", id)
        .Where("core.Status = 1");
        return await CreateDAO().FirstOrDefaultAsync<ViewContentAndContent>(sql);
    }


    /// <summary>
    /// 编辑使用 查询一条
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public async Task<EditContentAndContent> GetEditAsync(int? id)
    {
        Sql sql = Sql.Builder;
        sql.Select("*")
            .From("cxuu_content as core")
            .LeftJoin("cxuu_content_content as content")
            .On("core.Id=content.Aid")
            .Where("core.Id =@0", id);
        return await CreateDAO().FirstOrDefaultAsync<EditContentAndContent>(sql);
    }


    /// <summary>
    /// 根据CID 查询一条 用于判断当前栏目下有没有数据
    /// </summary>
    /// <param name="cid"></param>
    /// <returns></returns>
    public async Task<Content> GetContentByCidAsync(int cid)
    {
        Sql sql = Sql.Builder;
        sql.Select("*").From("cxuu_content").Where("Cid =@0", cid);
        return await CreateDAO().FirstOrDefaultAsync<Content>(sql);
    }

    /// <summary>
    /// 获取文章及栏目列表  后台使用
    /// </summary>
    /// <returns></returns>
    public async Task<Page<ViewContentAndCate>> GetListAsync(ContentQuery contentQuery)
    {
        Sql sql = Sql.Builder;
        sql.Select("cate.Name as CateName,dept.DeptName,u.Nickname,content.*")
            .From("cxuu_content as content")
            .LeftJoin("cxuu_content_cate as cate").On("content.Cid = cate.Id");
        sql.LeftJoin("cxuu_admin_user u").On("content.Uid=u.Id");
        sql.LeftJoin("cxuu_admin_dept dept").On("u.DeptId=dept.Id");

        if (contentQuery.Uids == "own")
            sql.Where("content.Uid = @0", contentQuery.Uid);
        else if (contentQuery.Uids != null)
            sql.Where("content.Uid IN (" + contentQuery.Uids + ")");

        if (contentQuery.Status != 2)
            sql.Where("content.Status = @0", contentQuery.Status);

        if (contentQuery.Cid != "0" && contentQuery.Cid != null)
            // sql.Where("content.Cid IN (" +contentQuery.Cid + ")");
            sql.Where("content.Cid =@0", int.Parse(contentQuery.Cid));

        if (!string.IsNullOrEmpty(contentQuery.Title))
            sql.Where("content.Title like @0", "%" + contentQuery.Title + "%");

        if (!string.IsNullOrEmpty(contentQuery.Orderby))
            sql.OrderBy("content." + contentQuery.Orderby);

        return await CreateDAO().PageAsync<ViewContentAndCate>(contentQuery.CurrentPage, contentQuery.Limit, sql);
    }

    /// <summary>
    /// 获取文章及栏目列表条数列表 前台使用
    /// </summary>
    /// <param name="cid"></param>
    /// <param name="limit"></param>
    /// <param name="orderBy"></param>
    /// <returns></returns>
    public async Task<List<ViewContentAndCate>> GetListByLimtAsync(ContentQuery contentQuery)
    {
        Sql sql = Sql.Builder;
        sql.Select("content.Id,cate.Name as CateName,content.Cid,content.Title,content.Examine,content.Img,content.Hits,content.Time,content.AttA,content.AttB,content.AttC")
            .From("cxuu_content as content")
            .LeftJoin("cxuu_content_cate as cate")
            .On("content.Cid = cate.Id")
            .Where("content.Status = 1");
        //属性
        if (contentQuery.Att == 1)
            sql.Where("content.AttA = 1");

        if (contentQuery.Att == 2)
            sql.Where("content.AttB = 1");

        if (contentQuery.Att == 3)
            sql.Where("content.AttC = 1");
        //图片
        if (contentQuery.ImgBl)
            sql.Where("content.Imgbl = 1");

        if (contentQuery.Cid != "0")
            sql.Where("content.Cid IN (" + contentQuery.Cid + ")");

        if (!string.IsNullOrEmpty(contentQuery.Orderby))
            sql.OrderBy("content." + contentQuery.Orderby);

        sql.Append("limit @0", contentQuery.Limit);
        return await CreateDAO().FetchAsync<ViewContentAndCate>(sql);
    }
    /// <summary>
    /// 前台搜索用
    /// </summary>
    /// <param name="page"></param>
    /// <param name="limit"></param>
    /// <param name="keyword"></param>
    /// <returns></returns>
    public async Task<Page<ViewContentAndCate>> SearchListAsync(int page, int limit, string keyword)
    {
        Sql sql = Sql.Builder;
        sql.Select("A.Id,B.Name as CateName,A.Cid,A.Title,A.Examine,A.Time,A.Status")
            .From("cxuu_content as A")
            .LeftJoin("cxuu_content_cate as B").On("A.Cid = B.Id");

        sql.Where("A.Status = 1");

        if (!string.IsNullOrEmpty(keyword))
        {
            sql.Where("A.Title like @0", "%" + keyword + "%");
        }

        sql.OrderBy("A.Id");

        return await CreateDAO().PageAsync<ViewContentAndCate>(page, limit, sql);
    }

    /// <summary>
    /// 插入一条信息
    /// </summary>
    /// <param name="content"></param>
    /// <param name="content"></param>
    /// <returns></returns>
    public async Task<Content> InsertContentAndContentAsync(Content content, ContentContent contentContent)
    {
        var dao = CreateDAO();
        await dao.OpenSharedConnectionAsync();
        try
        {

            if (content.Img != null)
                content.Imgbl = 1;

            var a = await dao.InsertAsync(content);

            if (contentContent.Content != null)
            {
                Sql sql = Sql.Builder;
                sql.Append("INSERT INTO cxuu_content_content (Aid, Content) VALUES (@0,@1);", content.Id, contentContent.Content);
                await CreateDAO().ExecuteAsync(sql);
            }

            if (a != null)
                return content;
        }
        finally
        {
            dao.CloseSharedConnection();
        }
        return null;
    }
    /// <summary>
    /// 更新一条信息
    /// </summary>
    /// <param name="content"></param>
    /// <param name="content"></param>
    /// <returns></returns>
    public async Task<int> UpdateContentAndContentAsync(Content content, ContentContent  contentContent)
    {
        if (content != null)
        {
            var dao = CreateDAO();
            if (content != null)
            {
                ContentContent ac = await dao.SingleOrDefaultAsync<ContentContent>(content.Id);
                if (ac != null)
                {
                    ac.Content = contentContent.Content;
                    await dao.UpdateAsync(ac);
                }
                else
                {
                    Sql sql = Sql.Builder;
                    sql.Append("INSERT INTO cxuu_content_content (Aid, Content) VALUES (@0,@1);", content.Id, contentContent.Content);
                    await CreateDAO().ExecuteAsync(sql);
                }
            }
            Content a = await dao.SingleOrDefaultAsync<Content>(content.Id);
            a.Title = content.Title;
            a.Status = content.Status;
            a.Img = content.Img;
            a.Imgbl = content.Img != null ? 1 : 0;
            a.Cid = content.Cid;
            a.AttA = content.AttA;
            a.AttB = content.AttB;
            a.AttC = content.AttC;
            a.Examine = content.Examine;
            a.Attments = content.Attments;
            return await dao.UpdateAsync(a);
        }
        return 0;
    }

    public async Task<int> UpdateContentAttAsync(int id, int status, string att)
    {
        Sql sql = Sql.Builder;
        switch (att)
        {
            case "AttA":
                sql.Append("SET AttA = @0 WHERE Id=@1", status, id);
                break;
            case "AttB":
                sql.Append("SET AttB = @0 WHERE Id=@1", status, id);
                break;
            case "AttC":
                sql.Append("SET AttC = @0 WHERE Id=@1", status, id);
                break;
            default:
                sql.Append("SET Status = @0 WHERE Id=@1", status, id);
                break;
        }
        return await CreateDAO().UpdateAsync<Content>(sql);
    }
}
